<%@ page import="java.io.InputStream" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.UUID" %><%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2021/3/23
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <%
      request.setCharacterEncoding("utf-8");
      /*String sname=request.getParameter("sname");
      String idcardpic=request.getParameter("idcardpic");
      out.print(sname+"<br/>"+idcardpic);*/
//      InputStream in=request.getInputStream();
//      int ch=0;
//      while((ch=in.read())!=-1){
//          out.print((char) ch);
//      }
      DiskFileItemFactory factory=new DiskFileItemFactory();
      ServletFileUpload upload=new ServletFileUpload(factory);
      upload.setHeaderEncoding("utf-8");
      List<FileItem> fileItems = upload.parseRequest(request);
      for (FileItem item : fileItems) {
          out.print("<p>");
          if(item.isFormField()){//普通表单域
              out.print(item.getFieldName()+",值："+item.getString("utf-8"));
          }else{//文件
              if(item.getSize()<1)
                  continue;
              //保存文件到服务器上
              out.print("文件名："+item.getName()+",大小："+item.getSize());
            String path=application.getRealPath("images");
            File f=new File(path);
            String fileName=item.getName();
            //换名字
              String houzhui=fileName.substring(fileName.lastIndexOf('.'));
              String newName= UUID.randomUUID()+houzhui;

            if(f.exists()==false)
                f.mkdirs();
            item.write(new File(f,newName));
            out.print(",文件保存在："+path+"下,名字叫："+newName);
          }
          out.print("</p>");
      }
  %>
</body>
</html>
